package com.tiantianyuegou.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.tiantianyuegou.R;
import com.tiantianyuegou.model.ProductSettingModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ivany on 2016/1/20.
 */
public class ProductSettingAdapter extends BaseAdapter {

    private Context mContext;
    private List<ProductSettingModel> mData;

    public ProductSettingAdapter(Context context) {
        mContext = context;
        mData = new ArrayList<>();
    }

    public ProductSettingAdapter(Context context, List<ProductSettingModel> data) {
        mContext = context;
        mData = data;
    }

    public void setData(List<ProductSettingModel> data) {
        mData.clear();
        mData.addAll(data);
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_product_setting, parent, false);
            holder = new ViewHolder();
            holder.mImg_arraw = (ImageView) convertView.findViewById(R.id.item_product_setting_img);
            holder.mImg_select = (ImageView) convertView.findViewById(R.id.item_product_setting_select);
            holder.mTxt_lable = (TextView) convertView.findViewById(R.id.item_peoduct_setting_txt);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        ProductSettingModel model = mData.get(position);
        if ("1".equals(model.getType())||"4".equals(model.getType())) {
            holder.mImg_select.setVisibility(View.GONE);
            holder.mImg_arraw.setVisibility(View.GONE);
        } else if ("2".equals(model.getType())) {
            holder.mImg_arraw.setVisibility(View.VISIBLE);
            holder.mImg_select.setVisibility(View.GONE);
        } else if ("3".equals(model.getType())) {
            holder.mImg_select.setVisibility(View.GONE);
            holder.mImg_arraw.setVisibility(View.GONE);
            if (model.isSelect()) {
                holder.mImg_select.setVisibility(View.VISIBLE);
            }
        }
        holder.mTxt_lable.setText(model.getLabel());
        return convertView;
    }

    class ViewHolder {
        ImageView mImg_arraw;
        ImageView mImg_select;
        TextView mTxt_lable;
    }
}
